package 力扣._3484_设计电子表格;

class Spreadsheet {

    int[][] data;

    public Spreadsheet(int rows) {
        data = new int[26][rows + 1];
    }

    public void setCell(String cell, int value) {
        int[] position = getPosition(cell);
        data[position[0]][position[1]] = value;
    }

    public void resetCell(String cell) {
        int[] position = getPosition(cell);
        data[position[0]][position[1]] = 0;
    }

    public int getValue(String formula) {
        int i = formula.indexOf('+');
        int v1 = getRealValue(formula.substring(1, i));
        int v2 = getRealValue(formula.substring(i + 1));
        return v1 + v2;
    }

    private int getRealValue(String v) {
        char c = v.charAt(0);
        if (c >= 'A' && c <= 'Z') {
            int i = c - 'A';
            int j = Integer.parseInt(v.substring(1));
            return data[i][j];
        }
        return Integer.parseInt(v);
    }

    private int[] getPosition(String cell) {
        int i = cell.charAt(0) - 'A';
        int j = Integer.parseInt(cell.substring(1));
        return new int[]{i, j};
    }
}

/**
 * Your Spreadsheet object will be instantiated and called as such:
 * Spreadsheet obj = new Spreadsheet(rows);
 * obj.setCell(cell,value);
 * obj.resetCell(cell);
 * int param_3 = obj.getValue(formula);
 */